<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
<cfcomponent output="false" extends="fileManager.util.AbsService" >
	
	<cffunction name="zip" access="public" returntype="Struct" >
		<cfargument name="source" type="string" required="true" hint="Absolute directory/file path."/>
		<cfargument name="destination" type="string" required="false" default="" hint="Absolute file path." />
		<cfset var result = structNew() />
		<cfset var fileName = "" />
		
		<cfset validatePermission( "zip" ) />
		
		<cfset arguments.source = getAbsolutePath( arguments.source ) />
		<cfset arguments.destination = arguments.source />
		
		<cfif fileExists( arguments.source ) >
		
			<cfset fileName = arguments.destination />
		
		<cfelse>
		
			<cfset fileName = arguments.destination & createUUID() />
		
		</cfif>
		
		<cftry>
			
			<cflock name="#arguments.source#" type="readonly" timeout="60" throwontimeout="true" >
			
				<cfzip action="zip" file="#fileName#.zip"
									source="#arguments.source#" 
									 />
			</cflock>
								 
			<cfset result['success'] = true />
									 
			<cfcatch type="any">
				
				<cfset result['success'] = false />
                <cfset result['type'] = "it.millemultimedia.fileManager.plugins.Archive" />
                <cfset result['detail'] = cfcatch.detail />
                <cfset result['message'] = cfcatch.message />
            	
				<cfset variables.eventManager.notifyOnError( error = result ) />
				
			</cfcatch>
		
		</cftry>
		
		<cfreturn result/>
	</cffunction>
	
	<cffunction name="unzip" access="public" returntype="Struct" >
		<cfargument name="source" type="string" required="true" hint="Absolute file path."/>
		<cfargument name="destination" type="string" required="false" default="" hint="Absolute directory path." />
		<cfset var result = structNew() />
		<cfset var fileName = "" />
		
		<cfset arguments.source = expandPath( arguments.source ) />
		<cfset arguments.destination = expandPath( arguments.source ) />
		
		<cftry>
		
			<cfif not listFind( 'zip,jar', listLast( arguments.source, '.' ), "," )>
				<cfthrow message="File tipe not supported." detail="Archive plug-in accept only ZIP and JAR files." />
			</cfif>
			
			<cflock name="#arguments.source#" type="readonly" timeout="60" throwontimeout="true" >
			
				<cfzip action="unzip" destination="#arguments.destination#"
									  file="#arguments.source#" 
									   />
			
			</cflock>
								 
			<cfset result['success'] = true />
									 
			<cfcatch type="any">
				
				<cfset result['success'] = false />
                <cfset result['type'] = "it.millemultimedia.fileManager.plugins.Archive" />
                <cfset result['detail'] = cfcatch.detail />
                <cfset result['message'] = cfcatch.message />
            	
				<cfset variables.eventManager.notifyOnError( error = result ) />
				
			</cfcatch>
		
		</cftry>
		
		<cfreturn result/>
	</cffunction>
	
</cfcomponent>